/*
 * @Author: liyanwei 15230656038@163.com
 * @Date: 2023-10-17 16:24:24
 * @LastEditors: liyanwei
 * @LastEditTime: 2023-10-17 17:35:31
 * @Description: file content
 */
import * as Cesium from 'cesium'
import { useGisStore } from '@/store/gisStore.js'

export default function () {
  let gisStore = useGisStore()
  Cesium.Ion.defaultAccessToken =
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxYWE5M2QzNy1hNGFjLTQ3YzItYmU0ZS05MDkyODc1MzVhNzAiLCJpZCI6MTE1MDQwLCJpYXQiOjE2Njg1OTA2NDh9.oW-_utGumUSPqYzlWGjhG8hbda-b4UxZdL0_2t4ASig'

  const viewer = new Cesium.Viewer('cesiumContainer', {
    infoBox: false,
    timeline: false, // 是否显示时间线控件
    selectionIndicator: false,
    imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
      url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
    })
    // terrain: Cesium.Terrain.fromWorldTerrain({
    //   requestVertexNormals: true, //Needed to visualize slope
    // }),
    // 指定上下文
    // contextOptions: {
    //   requestWebgl1: true,
    // },
  })
  viewer.scene.globe.depthTestAgainstTerrain = false
  viewer._cesiumWidget._creditContainer.style.display = 'none'
  viewer._baseLayerPicker._container.style.display = 'none'
  viewer._animation._container.style.display = 'none'
  gisStore.setViewer(viewer)
  console.log(viewer)
  // 去除logo
  viewer.cesiumWidget.creditContainer.style.display = 'none'

  // 显示帧率
  // viewer.scene.debugShowFramesPerSecond = true
  // viewer.scene.globe.depthTestAgainstTerrain = true

  // 外天空盒
  // viewer.scene.skyBox = new Cesium.SkyBox({
  //   sources: {
  //     positiveX: "/images/Standard-Cube-Map/px1.png",
  //     negativeX: "/images/Standard-Cube-Map/nx1.png",
  //     positiveY: "/images/Standard-Cube-Map/pz.png",
  //     negativeY: "/images/Standard-Cube-Map/nz1.png",
  //     positiveZ: "/images/Standard-Cube-Map/py.png",
  //     negativeZ: "/images/Standard-Cube-Map/ny1.png",
  //   },
  // });

  // 调试使用
  window.viewer = viewer

  // 监听点击事件，拾取坐标
  const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
  handler.setInputAction((e) => {
    const clickPosition = viewer.scene.camera.pickEllipsoid(e.position)
    const randiansPos = Cesium.Cartographic.fromCartesian(clickPosition)
    console.log(
      '经度：' +
        Cesium.Math.toDegrees(randiansPos.longitude) +
        ', 纬度：' +
        Cesium.Math.toDegrees(randiansPos.latitude)
    )
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
}
